home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / print / gsview10.zip / pipe.c < prev    next >
C/C++ Source or Header  |  1993-07-06  |  6KB  |  205 lines

  1. /*
  2.  * pipe.c --   Pipe support for GSVIEW.EXE, a graphical interface for 
  3.  *             MS-Windows Ghostscript
  4.  * Copyright (C) 1993  Russell Lang
  5.  *
  6.  * This program is free software; you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation; either version 2 of the License, or
  9.  * (at your option) any later version.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program; if not, write to the Free Software
  18.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  *
  20.  *   Author: Russell Lang
  21.  * Internet: rjl@monu1.cc.monash.edu.au
  22.  */
  23.  
  24. #define STRICT
  25. #include <windows.h>
  26. #include <windowsx.h>
  27. #include <commdlg.h>
  28. #include <shellapi.h>
  29. #include <mmsystem.h>
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <ctype.h>
  34. #include <dir.h>
  35. #include <io.h>
  36. #define NeedFunctionPrototypes 1
  37. #include "ps.h"
  38. #include "gsview.h"
  39.  
  40. /* routines to be visible outside this module */
  41. extern void pipeinit();
  42. extern FILE *pipeopen(void);    /* open pipe for first time */
  43. extern void pipeclose(void);    /* finished with pipe, close & delete temp files */
  44. extern void pipereset(void);    /* pipe is empty, do cleanup */
  45. extern void piperequest(void);    /* request from gswin for pipe data */
  46. extern void pipeflush(void);    /* start sending data through pipe */
  47. extern BOOL is_pipe_done(void);    /* true if pipe has just been reset */
  48.  
  49. /* internal to this module */
  50.  
  51. /* imitation pipes using SHAREABLE GLOBAL MEMORY */
  52. #define PIPE_DATASIZE 16384    /* maximum block size */
  53. /* Data is passed to gswin in a global shareable memory block.
  54.  * The global handle is passed in the LOWORD of lParam
  55.  * and the HIWORD contains the byte count.
  56.  * The maximum number of bytes passed is PIPE_DATASIZE.
  57.  * EOF is signified by count = 0 (hglobal must still be valid)
  58.  */
  59.  
  60. char pipe_name[MAXSTR];        /* pipe filename */
  61. FILE *pipe_file;        /* pipe file */
  62. fpos_t pipe_wpos;
  63. fpos_t pipe_rpos;
  64. BOOL pipe_empty;
  65. int piperead(char *buf, int size);
  66. char *pipebuf;
  67.  
  68. /* this is called before gswin is started */
  69. /* so we can tell when we get the first piperequest */
  70. void
  71. pipeinit(void)
  72. {
  73.     pipe_empty = FALSE;    /* so we wait for first request */
  74. }
  75.  
  76. FILE *
  77. pipeopen(void)
  78. {
  79.     if (pipe_file != (FILE *)NULL) {
  80.         fclose(pipe_file);
  81.         pipe_file = (FILE *)NULL;
  82.         unlink(pipe_name);
  83.         pipe_name[0] = '\0';
  84.     }
  85.     if ((pipe_file = gp_open_scratch_file(szScratch, pipe_name, "w+b")) == (FILE *)NULL) {
  86.         gserror(IDS_PIPEERR, NULL, NULL, SOUND_ERROR);
  87.         unlink(pipe_name);
  88.         pipe_name[0] = '\0';
  89.         return (FILE *)NULL;
  90.     }
  91.     pipebuf = malloc(PIPE_DATASIZE);
  92.     pipereset();
  93.     return pipe_file;
  94. }
  95.  
  96. void
  97. pipeclose(void)
  98. {
  99. HGLOBAL hglobal;
  100.     if (pipebuf != (char *)NULL) {
  101.         free(pipebuf);
  102.         pipebuf = (char *)NULL;
  103.     }
  104.     if (pipe_file != (FILE *)NULL) {
  105.         fclose(pipe_file);
  106.         pipe_file = (FILE *)NULL;
  107.         unlink(pipe_name);
  108.         pipe_name[0] = '\0';
  109.     }
  110.     if (hwndtext != (HWND)NULL) {
  111.         /* send an EOF (zero length block) */
  112.         hglobal = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, 1);
  113.         if (hglobal == (HGLOBAL)NULL) {
  114.             gserror(IDS_PIPEERR, NULL, NULL, SOUND_ERROR);
  115.             return;
  116.         }
  117.         PostMessage(hwndtext, WM_GSVIEW, PIPE_DATA, MAKELPARAM(hglobal,0));
  118.     }
  119.     pipe_empty = TRUE;
  120. }
  121.  
  122. /* rpos and wpos are now empty so reset the file */
  123. void
  124. pipereset(void)
  125. {
  126.     if ( (pipe_file == (FILE *)NULL) || (pipe_name[0] == '\0') )
  127.         return;
  128.     if ((pipe_file = freopen(pipe_name, "w+b", pipe_file)) == (FILE *)NULL) {
  129.         gserror(IDS_PIPEERR, NULL, NULL, SOUND_ERROR);
  130.         unlink(pipe_name);
  131.         pipe_name[0] = '\0';
  132.         return;
  133.     }
  134.     fgetpos(pipe_file, &pipe_rpos);
  135.     fgetpos(pipe_file, &pipe_wpos);
  136.     pipe_empty = TRUE;
  137.     info_wait(FALSE);
  138. }
  139.  
  140. /* give another block of data to gswin */
  141. /* called from WndImgProc */
  142. void
  143. piperequest(void)
  144. {
  145. HGLOBAL hglobal;
  146. LPBYTE lpb;
  147. UINT count;
  148.     if (pipe_file == (FILE *)NULL) {
  149.         pipe_empty = TRUE;
  150.         return;
  151.     }
  152.  
  153.     count = piperead(pipebuf, PIPE_DATASIZE);
  154.     if (count==0)
  155.         return;
  156.  
  157.     hglobal = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, PIPE_DATASIZE);
  158.     if (hglobal == (HGLOBAL)NULL) {
  159.         gserror(IDS_PIPEERR, NULL, NULL, SOUND_ERROR);
  160.         return;
  161.     }
  162.     lpb = GlobalLock(hglobal);
  163.     _fmemcpy(lpb, pipebuf, count);
  164.     GlobalUnlock(hglobal);
  165.     /* we may be processing SendMessage so use PostMessage to avoid lockups */
  166.     PostMessage(hwndtext, WM_GSVIEW, PIPE_DATA, MAKELPARAM(hglobal,count));
  167. }
  168.  
  169. /* write pipe_file to pipe */
  170. void
  171. pipeflush(void)
  172. {
  173.     if (pipe_empty) {
  174.         pipe_empty = FALSE;
  175.         piperequest();    /* repeat the request */
  176.     }
  177.     info_wait(TRUE);
  178. }
  179.  
  180. /* true  if pipereset was last called */
  181. /* false if pipeflush was last called */
  182. int
  183. is_pipe_done(void)
  184. {
  185.     return pipe_empty;
  186. }
  187.  
  188. /* read a block from pipe */
  189. /* return count of characters read */
  190. /* reset pipe if empty */
  191. int
  192. piperead(char *buf, int size)
  193. {
  194. int rcount;
  195.     fflush(pipe_file);
  196.     fgetpos(pipe_file,&pipe_wpos);
  197.     fsetpos(pipe_file,&pipe_rpos);
  198.     rcount = fread(buf, 1, size, pipe_file);
  199.     fgetpos(pipe_file,&pipe_rpos);
  200.     fsetpos(pipe_file,&pipe_wpos);
  201.     if (rcount == 0)
  202.        pipereset();
  203.     return rcount;
  204. }
  205.